﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using TeamsPortal.ServiceClient;
using Moq;
using System.IO;

namespace TeamsPortal.Tests
{
    [TestFixture]
    public class FileStorageTests
    {
        [Test]
        public void Save()
        {
            // Arrange
            const string fileName = "unitTest.txt";
            var bytes = new byte[6];
            Encoding.Unicode.GetEncoder().GetBytes(new char[] { '1', '2', '3' }, 0, 3, bytes, 0, true);
            const string storagePath = "TestStorage";
            
            var fileProviderMock = new Mock<IFileSystemProvider>();
            fileProviderMock.Setup(x => x.WriteAllBytes(It.IsAny<string>(), It.IsAny<byte[]>()));

            var storage = new FileStorageService(storagePath, fileProviderMock.Object);
            using (var stream = new MemoryStream(bytes))
            {
                //Act
                string relativePath = storage.SaveStream(stream, fileName);

                //Verify
                Assert.That(relativePath, Is.Not.Null.And.Not.StartsWith(storagePath));
            }

            fileProviderMock.VerifyAll();
            fileProviderMock.Verify(x => x.WriteAllBytes(It.IsAny<string>(), It.Is<byte[]>(p => p.SequenceEqual(bytes))),
                "Saved bytes are different from an initial array");
        }

        [Test]
        public void Load()
        {
            const string storagePath = "TestStorage";
            const string path = "unittest.txt";
            var fileProviderMock = new Mock<IFileSystemProvider>();
            fileProviderMock.Setup(x => x.OpenRead(Path.Combine(storagePath, path)));

            var storage = new FileStorageService(storagePath, fileProviderMock.Object);
            storage.LoadStream(path);

            fileProviderMock.VerifyAll();
        }
    }
}
